import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: user
 * Date: 2024-04-21
 * Time: 17:40
 */
public class Sort {
    public void insertSort(int[] array){
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i - 1;
            for (; j >= 0; j--) {
                if(tmp < array[j]){
                    array[j + 1] = array[j];
                }
                else{
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }
    public void shellSort(int[] array){
        int gap = array.length ;
        while(gap > 1){
            gap = gap / 2;
            shell(array,gap);
        }
    }
    public void shell(int[] array,int gap){
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i - gap;
            for (; j >= 0; j-=gap) {
                if(tmp < array[j]){
                    array[j+gap] = array[j];
                }
                else{
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }
    public void swap(int[] array,int i,int j){
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }
    public void selectSort1 (int[] array){
        for (int i = 0; i < array.length; i++) {
            int minIndex = i;

            for (int j = i + 1; j < array.length; j++) {

                if(array[j] < array[minIndex]){
                   minIndex = j;
                }

            }
            swap(array,minIndex,i);
        }
    }
    public void selectSort(int[] array){
        select(array,0,array.length - 1);
    }
    public void select(int[] array,int left,int right){
    while(left < right){
        int minIndex = left;
        int maxIndex = left;
        for (int i = left + 1; i < right ; i++) {
            if(array[i] < array[minIndex]){
                minIndex = i;
            }
            if(array[i] > array[maxIndex]){
                maxIndex = i;
            }
        }
        swap(array,left,minIndex);
        if(maxIndex == left){
            maxIndex = minIndex;
        }
        swap(array,right,maxIndex);
        left++;
        right--;
    }
    }
    public void heapSort(int[] array){
        createBigHeap(array);

        int end = array.length - 1;
        while( end >= 0) {
            swap(array, 0, end );
            siftDown(array,0,end);
            end--;
        }
    }
    public void createBigHeap(int[] array){
        for (int parent = (array.length - 1 - 1)/2; parent >= 0 ; parent--) {
            siftDown(array,parent,array.length );
        }
    }
    private void siftDown(int[] array,int parent,int end){
     int child = 2 * parent + 1;
     while(child < end){
         if(child + 1 < end&&array[child] < array[child + 1]){
             child++;
         }
         if(array[parent] < array[child]){
             swap(array,parent,child);
              parent = child;
             child = 2 * parent + 1;
         }else{
             break;
         }
     }
    }
    public void bubbleSort(int[] array){
        for (int i = 0; i < array.length; i++) {
            boolean flg = false;
            for (int j = 0; j < array.length - i - 1; j++) {
                if(array[j] > array[j + 1]){
                    swap(array,j,j + 1);

                }
                flg = true;
            }
            if(!flg){
                break;
            }
        }
    }
    public void insertSortRange(int[] array,int left,int right){
        for (int i = left + 1; i <= right; i++) {
            int tmp = array[i];
            int j = i - 1;
            for (; j >= left; j--) {
                if(tmp < array[j]){
                    array[j + 1] = array[j];
                }
                else{
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }
    public int midThreeNum(int[] array,int left,int right){
        int mid = (left + right) / 2;
        if(array[left] > array[right]){
            if(array[mid] > array[left]){
                return left;
            }
            else if(array[mid] < array[right]){
                return right;
            }
            else {
                return mid;
            }
        }else {
            if(array[mid] > array[right]){
                return right;
            }
            else if(array[mid] < array[left]){
                return left;
            }
            else {
                return mid;
            }
        }
    }
    public void quickSort(int[] array){
            quick(array,0,array.length - 1);

    }
    public void quick(int[] array,int start,int end){
        if (start >= end){
            return;
        }
        //三数取中法
        int index = midThreeNum(array,start,end);
        swap(array,start,index);
       // 在一定区间内使用插入排序
        if(end - start + 1 <= 10){
         insertSortRange(array,start,end);
         return;
        }
        int par = partition(array,start,end);
        quick(array,start,par - 1);
        quick(array,par + 1,end);
    }
    public int partitionHoare (int[] array,int start,int end){
        int left = start;
        int right = end;
        int key = array[left];

        while (left < right){
            while(left < right&&array[right] >= key){
                right--;
            }
            while (left < right&&array[left] <= key){
                left++;
            }

            swap(array,left,right);
        }
        swap(array,start,left);
        return right;
    }
   public int partitionPit(int[] array,int left,int right){
        int key = array[left];
        while(left < right){
            while(left < right&&array[right] >= key){
                right--;
            }
            array[left] = array[right];
            while (left < right&&array[left] <= key){
                left++;
            }
            array[right] = array[left];
        }
        array[right] = key;
        return right;
   }
    public int partition(int[] array,int left,int right){
        int prev = left;
        int cur = left + 1;
        while (cur < array.length){
            if(array[cur] < array[left]&&array[++prev] != array[cur]){
                swap(array,prev,cur);
            }
            cur++;
        }
        swap(array,left,prev);
        return prev;
    }
public void quickSortNor(int[] array){
        int left = 0;
        int right = array.length - 1;
        int par = partition(array,left,right);
    Stack<Integer> stack = new Stack<>();
    if(par > left + 1||par < right - 1){
        if(par > left + 1) {
            stack.push(left);
            stack.push(par - 1);
        }else {
            stack.push(par + 1);
            stack.push(right);
        }
    }
    while(!stack.isEmpty()){
        int end = stack.pop();
        int start = stack.pop();
        int m = partition(array,start,end);
        if(m > start + 1||m < end - 1){
            if(m > start + 1) {
                stack.push(start);
                stack.push(m - 1);
            }else {
                stack.push(m + 1);
                stack.push(end);
            }
        }
    }
}
public void mergeSort(int[] array){
    mergeSortFun(array,0,array.length - 1);

}
public void mergeSortFun(int[] array,int left,int right){
        if (left >= right){
            return;
        }
        int mid = (left + right) / 2;
    mergeSortFun(array,left,mid );
    mergeSortFun(array,mid + 1,right);
    //
    merge(array,left,mid,right);

}
private void merge(int[] array,int left,int mid,int right){
        int[] tmp = new int[right - left + 1];
        int s1 = left;
        int e1 = mid;
        int s2 = mid + 1;
        int e2 = right;
        int k = 0;
        while(s1 <= e1&&s2 <= e2){
            if(array[s1] < array[s2]){
                tmp[k++] = array[s1++];

            }
            else{
                tmp[k++] = array[s2++];

            }
        }
        while (s1 <= e1){
            tmp[k++] = array[s1++];
        }
        while (s2 <= e2){
            tmp[k++] = array[s2++];
        }
    for (int i = 0; i <  k; i++) {
        array[i + left] = tmp[i];
    }
}
    }
